#include "../include/netdisk.h"

int clientLogin(const char* ip, const char* port)
{
    int sockfd = tcpConnect(ip, port);

    int epfd = epoll_create(1);
    epollAdd(epfd, STDIN_FILENO);// 监听键盘输入

    train_t train = {0};
    char buf[4096] = {0};
    client_t client = {0};
    char salt[1024] = {0};

    int loginFlag = 0;// 0未连接服务端，1已连接服务端
    int passwordFlag = 1;// 控制消息输出
    int nameInput = 0; // 0用户名未输入，1用户名已输入
    int passwordInput = 0;// 0密码未输入，1密码已输入
    int linkCount = 0;// 连接计数，至多连接5次
    

    struct epoll_event readyset[1024];
    // 登录过程
    while(1)
    {
        epollAdd(epfd, sockfd);// 监听服务端消息
        if(loginFlag == 0)
        {// 未连接服务端，准备连接
            if(passwordFlag == 1)printf("正在连接服务端......\n");
            sockfd = tcpConnect(ip, port);
            if(sockfd < 0)
            {// 连接失败，继续连接
                close(sockfd);
                epollDel(epfd, sockfd);
                loginFlag = 0;
                sleep(1);
                linkCount++;
                if (linkCount == 5)
                {// 连接次数过多, 服务端断开
                    return -1;
                }                
                continue;
            }
            if(passwordFlag == 1)printf("连接服务端成功......\n");
            printf("\n请输入账号密码\n");
            nameInput = 0;      // 0用户名未输入，1用户名已输入
            passwordInput = 0;  // 0密码未输入，1密码已输入
            passwordFlag = 0;
            loginFlag = 1;
        }
        if (nameInput == 0)
        {
            printf("账号: ");
            fflush(stdout);
        }
        if (nameInput == 1 && passwordInput ==0)
        {          
            printf("密码: ");
            fflush(stdout);
        }        
        int readynum = epoll_wait(epfd, readyset, 1024, -1);// -1永久等待
        for (int i = 0; i < readynum; i++)
        {
            if (readyset[i].data.fd == STDIN_FILENO)
            {// 输入密码
                memset(&buf, 0, sizeof(buf));
                memset(&train, 0, sizeof(train));
                int readcount = read(STDIN_FILENO, buf, sizeof(buf));
                if(readcount <= 0)
                {// ctrl + d
                    printf("客户退出!\n");
                    return 1;
                }
                if (nameInput == 0)
                {// 账号未输入
                    memset(client.Name, 0, sizeof(client.Name));
                    strcpy(client.Name, strtok(buf, " \n"));
                    nameInput = 1;
                    continue;
                }
                if (passwordInput == 0)
                {// 密码未输入
                    memset(client.Password, 0, sizeof(client.Password));
                    strcpy(client.Password, strtok(buf, " \n"));
                    printf("test: client.Password = %s\n", client.Password);                    
                    // 加密
                    strncpy(client.Password, crypt(client.Password, salt), sizeof(client.Password));
                    printf("test: client.Password = %s\n", client.Password);                    
                    passwordInput = 1;
                }
                // 发送账号密码                    
                train.cmdstat = LOGIN;
                train.length = 2048;
                memcpy(train.data, &client, sizeof(client));                
                send(sockfd, &train, sizeof(train.cmdstat)+sizeof(train.length)+train.length, MSG_NOSIGNAL);
            }
            else if (readyset[i].data.fd == sockfd)
            {
                memset(&train, 0, sizeof(train));
                int recvcount = recv(sockfd, &train.cmdstat, sizeof(train.cmdstat), MSG_WAITALL);
                if(recvcount <= 0)
                {
                    printf("客户端test: 断开重连\n");
                    loginFlag = 0;
                    close(sockfd);
                    continue;
                }
                recv(sockfd, &train.length, sizeof(train.length), MSG_WAITALL);
                recv(sockfd, train.data, train.length, MSG_WAITALL);
                if (train.cmdstat & HASH)
                {// 接收盐值
                    memset(salt, 0 , sizeof(salt));
                    strncpy(salt, train.data, train.length);
                    printf("test: salt = %s\n", salt);
                    continue;
                }              

                if (train.cmdstat & IS_OK)
                {// 密码正确
                    printf("登录成功!\n");
                    printf("服务端test: %s\n", train.data);
                    // 登录成功自动调用ls命令
                    // 。。。。。。

                    return sockfd;                   
                }
                else
                {// 密码错误
                    printf("服务端test: %s\n", train.data);
                    continue;
                }
            }
        }       

    }
}